package com.xzj.admin.resource.sync.controller;

import com.xzj.admin.resource.sync.domain.vo.SelectPortOption;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import com.xzj.common.id.gen.DefaultIdHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.xzj.common.datasource.domain.PageReq;
import cn.hutool.core.util.ObjectUtil;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.admin.resource.sync.domain.entity.ResFieldsReflection;
import com.xzj.admin.resource.sync.service.ResFieldsReflectionService;

import java.util.List;
import java.util.Map;

/**
 * @program: xzj-admin
 * @ClassName: ResFieldsReflectionController
 * @description: 源到目标字段映射标;(res_fields_reflection)表控制层
 * @author: XiaZunJun
 * @create: 2024-10-31
 */
@Tag(name = "源到目标字段映射标对象功能接口")
@RestController
@RequestMapping("/resFieldsReflection")
public class ResFieldsReflectionController{
    @Resource
    private ResFieldsReflectionService resFieldsReflectionService;

    /**
     * 通过ID查询单条数据
     *
     * @param reflectionId 主键
     * @return 实例对象
     */
    @Operation(summary = "通过ID查询单条数据")
    @GetMapping("/queryById/{reflectionId}")
    public ResFieldsReflection queryById(@PathVariable(name = "reflectionId") String reflectionId){
        return resFieldsReflectionService.queryById(reflectionId);
    }

    /**
     * 分页查询
     *
     * @param pageReq 分页筛选条件对象
     * @return 查询结果
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pageQuery")
    public PageResp<ResFieldsReflection> pageQuery(@RequestBody PageReq<ResFieldsReflection> pageReq){
        return resFieldsReflectionService.pageQuery(pageReq);
    }

    /**
     * 新增数据
     *
     * @param resFieldsReflection 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public ResFieldsReflection addOrUpdate(@RequestBody ResFieldsReflection resFieldsReflection){
        if(ObjectUtil.isNotEmpty(resFieldsReflection.getReflectionId())){
            resFieldsReflectionService.update(resFieldsReflection);
        }else{
            resFieldsReflection.setReflectionId(DefaultIdHelper.nextIdStr());
            resFieldsReflectionService.insert(resFieldsReflection);
        }
        return resFieldsReflection;
    }

    /**
     * 通过主键删除数据
     *
     * @param reflectionId 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/deleteById/{reflectionId}")
    public boolean deleteById(@PathVariable(name = "reflectionId") String reflectionId){
        return resFieldsReflectionService.deleteById(reflectionId);
    }

    @Operation(summary ="通过源表ID查询其字段映射脚本")
    @GetMapping("/queryScriptBySourceId/{sourceId}")
    public Map<String, List<SelectPortOption>> queryScriptBySourceId(@PathVariable(name = "sourceId") String sourceId){
        return resFieldsReflectionService.queryScriptBySourceId(sourceId);
    }
}